package com.changgou.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.page.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private CategoryFeign categoryFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Value("${pagepath}")
    private String pagepath;

    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 生成商品详情静态化页面
     *      通过spuId获取Spu相关信息,category1Id....
     *      通过category1Id,category2Id,category3Id获取商品分类信息
     *      通过spuId获取sku列表信息
     *      通过Feign远程调用获取结果.
     * @param spuId
     */
    @Override
    public void generateItemPage(String spuId) {
        Map<String,Object> itemData = itemDataGenerate(spuId);
        //获取context对象,用于存放商品详情数据
        //thymeleaf包下的Context对象
        Context context = new Context();
        context.setVariables(itemData);
        //context.setVariable();
        //获取详情详情页生成位置
        File file = new File(pagepath);
        //判断商品详情页的文件夹是否存在,不存在创建
        if(!file.exists()){
            file.mkdirs();
        }
        //定义输出流,文件生成
        //生成的静态页面名称, path/123534223478237487234.html
        File fileName = new File(file+"/"+spuId+".html");
        Writer writer = null;
        try {
            writer = new PrintWriter(fileName);

            //通过template对象来生成静态化页面
            //参数1,模板名称,指定模板名称即可,item.html,模板名称item.
            //参数2,context对象,模板中需要的数据.
            //参数3,输出流,指定的文件位置.
            templateEngine.process("item",context,writer);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Map<String,Object> itemDataGenerate(String spuId) {
        Map<String,Object> resultMap = new HashMap<>();

        //准备数据
        //商品Spu数据
        Spu spuData = (Spu) spuFeign.findById(spuId).getData();
        resultMap.put("spu",spuData);

        //商品图片信息
        if(spuData.getImages() != null){
            String[] imageArray = spuData.getImages().split(",");
            resultMap.put("imageList",imageArray);
        }

        //商品分类数据,三级分类
        //获取分类Id
        Integer category1Id = spuData.getCategory1Id();
        Integer category2Id = spuData.getCategory2Id();
        Integer category3Id = spuData.getCategory3Id();
        String category1Name = ((Category)categoryFeign.findById(category1Id).getData()).getName();
        String category2Name = ((Category)categoryFeign.findById(category2Id).getData()).getName();
        String category3Name = ((Category)categoryFeign.findById(category3Id).getData()).getName();
        resultMap.put("category1Name",category1Name);
        resultMap.put("category2Name",category2Name);
        resultMap.put("category3Name",category3Name);

        //商品sku列表数据
        List<Sku> skuList = (List<Sku>) skuFeign.findSkuListBySpuId(spuId).getData();
        resultMap.put("skuList",skuList);

        Map specMap = JSON.parseObject(spuData.getSpecItems(), Map.class);
        resultMap.put("specificationList",specMap);

        return resultMap;

    }
}
